﻿using System;
using System.Web.Mvc;
using CRMSystemV2.Common;
using System.IO;
namespace CRMSystemV2.MVCWeb.Controllers
{
    public class SysController : BaseController
    {
        //导出用自定义列头
        private readonly string[] arrExportColumns = new string[] { "ID", "用户名", "工号", "邮箱", "电话", "是否可用" };
        //关键字
        private readonly string key_username = "username";
        private readonly string key_jobno = "jobno";
        private readonly string key_email = "email";
        private readonly string key_phone = "phone";
        private readonly string key_isenable = "isenable";
        //
        // GET: /Sys/

        public ActionResult User()
        {
            //获取搜索关键字
            SetValue(key_username, Request[key_username] == null ? "" : Request[key_username]);
            SetValue(key_jobno, Request[key_jobno] == null ? "" : Request[key_jobno]);
            SetValue(key_email, Request[key_email] == null ? "" : Request[key_email]);
            SetValue(key_phone, Request[key_phone] == null ? "" : Request[key_phone]);
            SetValue(key_isenable, Request[key_isenable] == null ? "" : Request[key_isenable]);
            //分页信息
            ViewData["user"] = api.UserPageView(out rowTotal, out pageTotal, pageSize, pageIndex, keyWords, orderField, orderDirection);
            ViewData["pageIndex"] = pageIndex;
            ViewData["pageSize"] = pageSize;
            ViewData["rowTotal"] = rowTotal;
            ViewData["orderField"] = orderField;
            ViewData["orderDirection"] = orderDirection;
            //保存搜索关键字
            ViewData[key_username] = GetValue(key_username);
            ViewData[key_jobno] = GetValue(key_jobno);
            ViewData[key_email] = GetValue(key_email);
            ViewData[key_phone] = GetValue(key_phone);
            ViewData[key_isenable] = GetValue(key_isenable);

            return View();
        }
        //添加视图
        public ActionResult UserAdd()
        {
            return View();
        }
        //添加操作
        public JsonResult UserAddDone()
        {
            int isenable = int.Parse(Request["isenable"]);
            string username = Request["username"],
                jobno = Request["jobno"],
                email = Request["email"],
                phone = Request["phone"];

            string result = api.UserInsert(username, jobno, email, phone, isenable);
            ResultJson rj = result.DeserializeObject();
            ajaxDone json = new ajaxDone()
            {
                statusCode = rj.status,
                message = rj.message,
                navTabId = "User",
                callbackType = "closeCurrent"
            };
            return Json(json);
        }
        //修改视图
        public ActionResult UserEdit()
        {
            if (Request["id"] != null)
                ViewData["user"] = api.GetUser(Request["id"]); ;
            return View();
        }
        //修改操作
        public JsonResult UserEditDone()
        {
            int id = int.Parse(Request["id"]), isenable = int.Parse(Request["isenable"]);
            string username = Request["username"],
                jobno = Request["jobno"],
                email = Request["email"],
                phone = Request["phone"];

            string result = api.UserUpdate(id, username, jobno, email, phone, isenable);
            ResultJson rj = result.DeserializeObject();
            ajaxDone json = new ajaxDone()
            {
                statusCode = rj.status,
                message = rj.message,
                navTabId = "User",
                callbackType = "closeCurrent"
            };
            return Json(json);
        }
        //查询视图
        public ActionResult UserSearch()
        {
            return View();
        }
        //删除操作,支持批量删除
        public JsonResult UserDelete()
        {
            string result = api.UserDelete(Request["ids"]);
            ResultJson rj = result.DeserializeObject();
            ajaxDone json = new ajaxDone()
            {
                statusCode = rj.status,
                message = rj.message,
                navTabId = "User"
            };
            return Json(json);
        }
        //重置密码
        public JsonResult ResetPassword()
        {
            string result = api.ResetPassword(Request["id"]);
            ResultJson rj = result.DeserializeObject();
            ajaxDone json = new ajaxDone()
            {
                statusCode = rj.status,
                message = rj.message
            };
            return Json(json);
        }
        //导出excel
        public ActionResult UserExport()
        {
            int maxCount = int.Parse(HttpContext.Application["ExportCount"].ToString());
            System.Data.DataTable dt = api.UserExportTable(maxCount, keyWords, orderField, orderDirection);
            string fileName = string.Format("用户信息{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));
            string filePath = string.Format("{0}/{1}",FileController.DownloadPath, fileName);
            dt.Export2Excel(null, arrExportColumns, filePath);
            //下载文件
            return RedirectToRoute(new { controller = "File", action = "Download", file = fileName });
        }
    }
}
